/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.d.ui.view.advertswitcher;

import com.d.lib.common.widget.TitleLayout;
import com.d.lib.ui.view.advertswitcher.AdvertSwitcher;
import com.d.ui.view.ResourceTable;
import java.util.ArrayList;
import java.util.List;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;

public class AdvertSwitcherAbilitySlice extends AbilitySlice {

  private AdvertSwitcher as_advert_text, as_advert_img;

  @Override
  protected void onStart(Intent intent) {
    super.onStart(intent);
    super.setUIContent(ResourceTable.Layout_ability_advert);

    bindView();
    initConfig();
  }

  private void bindView() {
    TitleLayout titleLayout = (TitleLayout) findComponentById(ResourceTable.Id_tl_title);
    titleLayout.setBackClickListener(this::onBackClick);

    as_advert_text = (AdvertSwitcher) findComponentById(ResourceTable.Id_as_advert_text);
    as_advert_img = (AdvertSwitcher) findComponentById(ResourceTable.Id_as_advert_img);
  }

  private void initConfig() {
    AdvertSwitcherTextAdapter textAdapter = new AdvertSwitcherTextAdapter(this, getTextDataset(),
        ResourceTable.Layout_adapter_advert_text);
    as_advert_text.setAdapter(textAdapter);

    AdvertSwitcherImgAdapter imgAdapter = new AdvertSwitcherImgAdapter(this, getImgDataset(),
        ResourceTable.Layout_adapter_advert_img);
    as_advert_img.setAdapter(imgAdapter);
  }

  @Override
  protected void onActive() {
    super.onActive();
    as_advert_text.start();
    as_advert_img.start();
  }

  @Override
  protected void onInactive() {
    super.onInactive();
    as_advert_text.stop();
    as_advert_img.stop();
  }

  private List<AdvertSwitcherBean> getTextDataset() {
    List<AdvertSwitcherBean> dataSet = new ArrayList<>();
    for (int i = 0; i < 6; i++) {
      dataSet.add(new AdvertSwitcherBean("Hot " + i, "Popular events!!!", 0));
    }
    return dataSet;
  }

  private List<AdvertSwitcherBean> getImgDataset() {
    List<AdvertSwitcherBean> dataSet = new ArrayList<>();
    for (int i = 0; i < 16; i++) {
      dataSet.add(new AdvertSwitcherBean("" + i, i + ". Promotions",
          ResourceTable.Media_lib_pub_ic_btb_icon));
    }
    return dataSet;
  }

  public void onBackClick(Component v) {
    terminate();
  }
}
